Page({
  data: {
    loadingHidden: "0",
    flag: 0,
    is_class: 0
  },
  onLoad: function(options) {
    var is_class = options.is_class;
    var that = this;
    wx.showToast({
      title: '此服务需要连接校园网',
      icon: 'none',
      duration: 2000
    });
    var user_id = wx.getStorageSync('user_id');
    that.setData({
      user_id: user_id,
      is_class: is_class
    })

    wx.request({
      url: 'https://api.geek51.cn/users/' + user_id + '/',
      method: "GET",
      success: function(res) {
        wx.setStorageSync('username', res.data.data.student_id);
        if (res.data.data.student_id != 0) {
          that.setData({
            username: res.data.data.student_id,
            flag: 1
          })
        }
      }
    })
  },

  onShow: function(){
    wx.setStorageSync('is_class', 0);
  },

  onBindStudent_id: function(e) {
    var username = e.detail.value;
    this.setData({
      username: username
    })
  },
  onBindPassword: function(e) {
    var password = e.detail.value;
    this.setData({
      password: password
    })
  },
  change_jiaowu: function(e) {
    var that = this;
    wx.request({
      url: 'https://net.geek51.cn/api/zhjw/binding/',
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        is_flush: 1,
        username: that.data.username,
        password: that.data.password
      },
      success: function(res) {
        wx.hideLoading();
        if (res.statusCode == 7) {
          wx.showModal({
            title: '提示',
            content: '请将手机连接校园网后重试!',
            showCancel: false
          })
        } else {
          if (res.data.error == 0) {
            wx.showModal({
              title: '提示',
              content: '更改成功！',
              showCancel: false
            })
          } else {
            wx.showModal({
              title: '提示',
              content: '密码错误！',
              showCancel: false
            })
          }
        }
      },
      fail: function (res) {
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '请将手机连接校园网后重试!',
          showCancel: false
        })
      }
    })
  },
  bind_to_jiaowu: function(e) {
    var that = this;
    wx.showLoading({
      title: '认证中'
    })
    wx.request({
      url: 'https://net.geek51.cn/api/zhjw/binding/',
      method: "POST",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        'username': that.data.username,
        'password': that.data.password
      },
      success: function(res) {
        wx.hideLoading();
        console.log(res);
        if (res.statusCode == 7) {
          wx.showModal({
            title: '提示',
            content: '请将手机连接校园网后重试!',
            showCancel: false
          })
        } else {
          var error = res.data.error;
          if (error == -1) {
            wx.hideLoading();
            wx.showModal({
              title: '提示',
              content: '密码错误！',
              showCancel: false,
              success: res => {
                if (res.confirm) {
                  that.setData({
                    newPassword: ''
                  })
                }
              }
            })
          } else {
            wx.request({
              url: 'https://api.geek51.cn/users/' + that.data.user_id + '/',
              method: "PUT",
              data: {
                student_id: that.data.username
              },
              success: function(res) {
                wx.hideLoading();
                console.log(res.data.error)
                wx.showModal({
                  title: '提示',
                  content: '认证成功！',
                  showCancel: false,
                  success: res => {
                    if (res.confirm) {
                      if (that.data.is_class == 1) {
                        wx.setStorageSync('is_class', 1);
                        wx.setStorageSync('username', that.data.username);
                        wx.switchTab({
                          url: '/pages/classable/classable'
                        })
                      } else {
                        wx.switchTab({
                          url: '/pages/my/my'
                        })
                      }

                    }
                  }
                })
              }
            })
          }
        }
      },
      fail: function(res){
        wx.hideLoading();
        wx.showModal({
          title: '提示',
          content: '请将手机连接校园网后重试!',
          showCancel: false
        })
      }
    })
  }
})